package com.sjh.sjhliving.commodity.service.impl;

import com.sjh.sjhliving.commodity.vo.Catalog2Vo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sjh.common.utils.PageUtils;
import com.sjh.common.utils.Query;

import com.sjh.sjhliving.commodity.dao.CategoryDao;
import com.sjh.sjhliving.commodity.entity.CategoryEntity;
import com.sjh.sjhliving.commodity.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listTree() {

        //1、查出所有分类数据
        List<CategoryEntity> entities = baseMapper.selectList(null);


        List<CategoryEntity> categoryTree = entities.stream().filter(categoryEntity -> categoryEntity.getParentId() == 0)
                .map(categoryEntity -> {
                    categoryEntity.setChildrenCategories(getChildrenCategories(categoryEntity, entities));
                    return categoryEntity;
                }).sorted((category1, category2) -> {
                    return (category1.getSort() == null ? 0 : category1.getSort()) -
                            (category2.getSort() == null ? 0 : category2.getSort());
                }).collect(Collectors.toList());
        return categoryTree;
    }

    @Override
    public Long[] getCascadedCategoryId(Long categoryId) {


        List<Long> cascadedCategoryId = new ArrayList<>(); // [用来收集 categoryId 的父Id]
        getParentCategoryId(categoryId, cascadedCategoryId);
        Collections.reverse(cascadedCategoryId);
        return cascadedCategoryId.toArray(new Long[cascadedCategoryId.size()]);
    }

    @Override
    public List<CategoryEntity> getLevel1Categorys() {

        List<CategoryEntity> categoryEntities = this.baseMapper.selectList(
                new QueryWrapper<CategoryEntity>().eq("parent_id", 0));
        return categoryEntities;
    }

    @Override
    public Map<String, List<Catalog2Vo>> getCatalogJson() {

        // 1.获得所有的商品类目
        List<CategoryEntity> selectList = this.baseMapper.selectList(null);
        // 2.获得所有的一级分类
        List<CategoryEntity> categoryLevel1Entities = getParent_cid(selectList, 0L);
        // 3. 流式计算将 list -> map
        /*key 是一级分类的Id*/
        /*value 是List<Catalog2Vo> */
        Map<String, List<Catalog2Vo>> res = categoryLevel1Entities.stream().collect(Collectors.toMap(k->k.getId().toString(), v ->{
            // 3.1 获取一级分类下的二级分类
            List<CategoryEntity> categoryLevel2Entities = getParent_cid(selectList, v.getId());
            List<Catalog2Vo> cataV02list = null;
            if(categoryLevel2Entities != null && categoryLevel2Entities.size() > 0){
                cataV02list = categoryLevel2Entities.stream().map(l2 -> {
                    Catalog2Vo catalog2Vo = new Catalog2Vo(v.getId().toString(), l2.getId().toString(), l2.getName(), null);
                    //获取二级分类下的三级分类
                    List<CategoryEntity> categoryLevel3Entities = getParent_cid(selectList, l2.getId());
                    if(categoryLevel3Entities != null && categoryLevel3Entities.size() > 0){
                        List<Catalog2Vo.Category3Vo> category3VoList = categoryLevel3Entities.stream().map(l3->{
                            Catalog2Vo.Category3Vo category3Vo = new Catalog2Vo.Category3Vo(l2.getId().toString(), l3.getId().toString(), l3.getName());
                            return category3Vo;
                        }).collect(Collectors.toList());
                        catalog2Vo.setCatalog3List(category3VoList);
                    }
                    return catalog2Vo;
                }).collect(Collectors.toList());
            }
            return cataV02list;
        }));
        return res;
    }

    private List<CategoryEntity> getParent_cid(List<CategoryEntity> selectList,Long parentCid) {

        return selectList.stream().filter(item -> item.getParentId().equals(parentCid)).collect(Collectors.toList());
    }

    public void getParentCategoryId(Long categoryId, List<Long> cascadedCategoryId){

        cascadedCategoryId.add(categoryId);
        CategoryEntity categoryEntity = this.getById(categoryId);
        if(categoryEntity.getParentId() != 0){
            getParentCategoryId(categoryEntity.getParentId(), cascadedCategoryId);
        }
    }

    public List<CategoryEntity> getChildrenCategories(CategoryEntity root, List<CategoryEntity> all){

        List<CategoryEntity> children = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentId().longValue() == root.getId().longValue();
        }).map(categoryEntity -> {
            categoryEntity.setChildrenCategories(getChildrenCategories(categoryEntity, all));
            return categoryEntity;
        }).sorted((category1, category2) -> {
            return (category1.getSort() == null ? 0 : category1.getSort()) -
                    (category2.getSort() == null ? 0 : category2.getSort());
        }).collect(Collectors.toList());
        return children;
    }



}